package com.jdls.day12.work;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * @author 嘉德罗斯
 * @date 2021/8/2
 */
public class Test2 {
    public static void main(String[] args) throws IOException {
        //键盘录入一个任意字符串，
        // 统计字符串中相邻字符出现的次数，将结果写到D盘下的count.txt文件中（面试题）
        //    例如：aaccaabaab
        //    count.txt：2a2c2a1b2a1b

        Scanner sc = new Scanner(System.in);
        FileOutputStream fo = new FileOutputStream("jdls-class\\count1.txt");
        String s = "aaaccaabaab";
        char[] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            int a = 1;
            if (i + 1 == c.length) {
                byte[] b = (a + "" + c[i]).getBytes();
                fo.write(b);
                break;
            }
            if (c[i] == c[i + 1]) {
                a++;
                byte[] b = (a + "" + c[i]).getBytes();
                fo.write(b);
                i = i + 1;
            } else {
                a = 1;
                byte[] b = (a + "" + c[i]).getBytes();
                fo.write(b);
            }
        }
        FileInputStream fis = new FileInputStream("jdls-class\\count1.txt");
        while (true) {
            int a = fis.read();
            if (a == -1) {
                break;
            } else {
                System.out.print((char) a);
            }
        }
        fo.close();
        fis.close();
    }
}
